package com.lzl.service;


import com.lzl.constant.CommonStatusEnum;
import com.lzl.constant.IdentityConstant;
import com.lzl.constant.TokenConstant;
import com.lzl.responese.ResponseResult;
import com.lzl.remote.ServicePassengerUserClient;
import com.lzl.remote.ServiceVerificationCodeClient;
import com.lzl.request.VerificationCodeDTO;
import com.lzl.responese.NumberCodeResponese;
import com.lzl.responese.TokenResponse;
import com.lzl.util.JWTUtils;
import com.lzl.util.RedisPrefixUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class VerificationCodeService {

    private  static String verificationCodePrefix="passenger-verification-code-";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;


    public ResponseResult generatorCode(String passengerPhone){
        //调用验证服务，获取验证码
        ResponseResult<NumberCodeResponese> code = serviceVerificationCodeClient.getNumberCode(6);
        int numberCode = code.getData().getNumberCode();
        System.out.println("调用验证服务，获取验证码"+numberCode);
        String key=verificationCodePrefix+passengerPhone;
        //存入redis
        System.out.println("存入redis");
        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);

        NumberCodeResponese numberCodeResponese = new NumberCodeResponese();
        numberCodeResponese.setNumberCode(numberCode);

        return ResponseResult.success(numberCodeResponese);
    }

    public ResponseResult checkCode(String passengerPhone,String verificationCode){

        //获取redis中的验证码
        String codeRedis = stringRedisTemplate.opsForValue().get(verificationCodePrefix + passengerPhone);

        //校验验证码
        //redis中的验证码已经过期
        if(StringUtils.isBlank(codeRedis)){
            return ResponseResult.fail(CommonStatusEnum.VERFICATION_CODE_BLANK.getCode(),CommonStatusEnum.VERFICATION_CODE_BLANK.getValue());
        }
        //注意：此处用trim（）容易报空指针问题
        if(!codeRedis.trim().equals(verificationCode)){
            //验证码错误
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }

        //判断是否存在用户，并进行处理
        VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
        verificationCodeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);

        String accessToken= JWTUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY,TokenConstant.ACCESS_TOKEN);
        String refreshToken = JWTUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstant.REFRESH_TOKEN);
        //将token存入redis中
        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone,IdentityConstant.PASSENGER_IDENTITY, TokenConstant.ACCESS_TOKEN);
        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone,IdentityConstant.PASSENGER_IDENTITY, TokenConstant.REFRESH_TOKEN);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,3,TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,6,TimeUnit.MINUTES);


        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }


}
